package zerous.taphero.util;

import android.content.*;
import java.io.*;
import android.util.*;
import android.os.*;
import java.util.*;

public class IOHelper {
	public static File myPath = new File(Environment.getExternalStorageDirectory(), "TapHero");
	public static File charas = new File(myPath, "characters.data");
	
	
	public static boolean isExists() {
		return false;
	}
	
	public static String readData(Context context, String fileName) {
		FileInputStream is = null;
		String result = null;
		
		try {
			File file = new File(myPath, fileName);
			is = new FileInputStream(file);
			BufferedInputStream bf = new BufferedInputStream(is);
			byte[] data = new byte[is.available()];
			bf.read(data);
			result = new String(data);
		} catch (IOException e) {
			
		}
		return result;
	}
	
	public static String[] readCharacters(Context context) {
		FileInputStream is = null;
		String charasStr = null;
		try {
			is = new FileInputStream(charas);
			byte[] data = new byte[is.available()];
			is.read(data);
			charasStr = new String(data);
		} catch (IOException e) {
			Log.e("I", "Can't read that");
		}
		return charasStr.split(",");
	}
	
	public static void writeCharacters(Context context, List<String> characters) {
		try {
			FileOutputStream os = new FileOutputStream(charas);
			StringBuilder builder = new StringBuilder();
			for(String s:characters) {
				builder.append(s);
				builder.append(",");
			}
			BufferedOutputStream bf = new BufferedOutputStream(os);
			bf.write(builder.toString().getBytes());
			bf.close();
		} catch(Exception e) {

		}
	}
	
	public static void writeData(Context context, String fileName, byte[] src) {
		try {
			File file = new File(myPath, fileName);
			FileOutputStream os = new FileOutputStream(file);
			BufferedOutputStream bf = new BufferedOutputStream(os);
			bf.write(src);
			bf.close();
		} catch(Exception e) {
			
		}
	}
}
